CC=gcc
INCLUDE_PATHS =-I.
CFLAGS=

BUILD_DIR=build

LIB_NAME=my_math_lib

STATIC_MAIN=my_main_statically_linked
SHARED_MAIN=my_main_shared_linked

LIB_OBJS = \
	my_math_lib.o

DEPS = \
	my_math_lib.h

%.o: %.c $(DEPS)
	@$(CC) -c -o $(BUILD_DIR)/$@ $< $(CFLAGS) 

.PHONY: my_main_static my_main_dynamic clean
all: my_main_static my_main_shared

static_lib: $(LIB_OBJS)
	@ar rcs lib$(LIB_NAME).a $(BUILD_DIR)/*.o
	@echo ""
	@echo "    Created static library $(LIB_NAME) --> lib$(LIB_NAME).a"

shared_lib:
	@gcc -c -fPIC my_math_lib.c -o $(BUILD_DIR)/my_math_lib.o
	@gcc -shared -Wl,-soname,lib$(LIB_NAME).so.1 -o lib$(LIB_NAME).so.1.0.1  $(BUILD_DIR)/*.o
	@echo ""
	@echo "    Created shared library $(LIB_NAME) --> lib$(LIB_NAME).so.1.0.1"

my_main_static: static_lib
	@echo "    Compiling main as static binary..."
	@gcc -static mymain.c -L. -l$(LIB_NAME) -o $(STATIC_MAIN)
	@echo "    Created [ $(STATIC_MAIN) ] of size `ls -l $(STATIC_MAIN) | awk '{print $$5}'`"

my_main_shared: shared_lib
	@echo "    Compiling main as shared binary..."
	@gcc mymain.c -o $(SHARED_MAIN) -L. -l$(LIB_NAME)
	@echo "    Created [ $(SHARED_MAIN) ] ] of size `ls -l $(SHARED_MAIN) | awk '{print $$5}'`"
	@echo "    $(SHARED_MAIN) uses the following shared libraries..."
	@ldd $(SHARED_MAIN)
	@echo ""

clean:
	@rm -f $(BUILD_DIR)/*.o *~ core *.a *_linked libmy_math*
	@echo "    Cleaning directories and object files."
